package com.ktjiaoyu.news.dao.impl;

import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.UserDao;
import com.ktjiaoyu.news.entity.NewsType;
import com.ktjiaoyu.news.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    List<Object> params=new ArrayList<>();
    @Override
    public List<User> getPage(User user, Integer pageNo, Integer pageSize) {
        List<User> list=new ArrayList<User>();
        try {
            conn=getConnection();

            String sql="select * from t_user where 1=1 and enabled = 0";
            sql+=createPageCondition(user,params);
            sql+=" limit ?,? ";
            params.add((pageNo-1)*pageSize);
            params.add(pageSize);
            ps=conn.prepareStatement(sql);
            for(int i=0;i<params.size();i++){
                ps.setObject(i+1,params.get(i));
            }
            rs=ps.executeQuery();
            while (rs.next()){
                User user1=new User();
                user1.setId(rs.getInt("id"));
                user1.setRoleId(rs.getInt("roleId"));
                user1.setLoginName(rs.getString("loginName"));
                user1.setUserName(rs.getString("userName"));
                user1.setPassword(rs.getString("password"));
                user1.setSex(rs.getInt("sex"));
                user1.setIdentityCode(rs.getString("identityCode"));
                user1.setEmail(rs.getString("email"));
                user1.setMobile(rs.getString("mobile"));
                user1.setEnabled(rs.getInt("enabled"));
                user1.setCreateTime(rs.getString("create_time"));
                user1.setUpdateTime(rs.getString("update_time"));
                user1.setAvatar(rs.getString("avatar"));
                list.add(user1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn,ps,rs);
        }
        return list;
    }

    @Override
    public int count() {
        int count=0;
        try {
            conn=getConnection();
            String sql="select count(*) from t_user where 1=1 and enabled = 0";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if (rs.next()) {
               count= rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn,ps,rs);
        }
        return count;
    }

    @Override
    public List<User> getAll() {
        return Collections.emptyList();
    }

    @Override
    public User getByIdUser(Integer id) {
        User user=null;
        try {
            // 获取连接
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from t_user where id=? ";
            // 获取执行sql对象，设置参数
            ps = conn.prepareStatement(sql);
            ps.setObject(1, id);
            // 执行获得结果
            rs = ps.executeQuery();
            while(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setRoleId(rs.getInt("roleId"));
                user.setLoginName(rs.getString("loginName"));
                user.setUserName(rs.getString("userName"));
                user.setPassword(rs.getString("password"));
                user.setSex(rs.getInt("sex"));
                user.setIdentityCode(rs.getString("identityCode"));
                user.setEmail(rs.getString("email"));
                user.setMobile(rs.getString("mobile"));
                user.setEnabled(rs.getInt("enabled"));
                user.setCreateTime(rs.getString("create_time"));
                user.setUpdateTime(rs.getString("update_time"));
                user.setAvatar(rs.getString("avatar"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, ps, rs);
        }
        return user;
    }

    @Override
    public int insertUser(User user) {
        return executeUpdate("INSERT INTO t_user(roleId,loginName,userName,`password`,sex,identityCode,email,mobile,create_time,update_time,enabled) VALUES(?,?,?,?,?,?,?,?,NOW(),NOW(),0);"
                ,user.getRoleId(),user.getLoginName(),user.getUserName(),user.getPassword(),user.getSex(),user.getIdentityCode(),user.getEmail(),user.getMobile());
    }

    @Override
    public int deleteUser(Integer id) {
        return executeUpdate("delete from t_user where id=?", id);
    }

    @Override
    public int update(User user) {
        if(user == null || user.getId() == null){
            return 0;
        }
        List<Object> params = new ArrayList<>();
        String sql = "UPDATE `t_user` SET ";
        if(user.getLoginName() != null && !user.getLoginName().trim().isEmpty()){
            sql += " loginName = ? ,";
            params.add(user.getLoginName());
        }
        if(user.getPassword() != null && !user.getPassword().trim().isEmpty()){
            sql += " password = ? ,";
            params.add(user.getPassword());
        }
        if(user.getSex()!=null){
            sql+=" sex = ? ,";
            params.add(user.getSex());
        }
        if (user.getIdentityCode()!=null && !user.getIdentityCode().trim().isEmpty()) {
            sql+=" identityCode = ? ,";
            params.add(user.getIdentityCode());
        }
        if(user.getEmail()!=null && !user.getEmail().trim().isEmpty()){
            sql+=" email = ? ,";
            params.add(user.getEmail());
        }
        if(user.getMobile()!=null && !user.getMobile().trim().isEmpty()){
            sql+=" mobile = ? ,";
            params.add(user.getMobile());
        }
        if(user.getEnabled()!=null && !user.getCreateTime().trim().isEmpty()){
            sql+=" enabled = ? ,";
            params.add(user.getEnabled());
        }
        sql+=" update_time = Now() ";
        if(params.size() > 0){
            // 去除逗号
            sql = sql.substring(0, sql.length() - 1);
            sql += " WHERE id = ? ";
            params.add(user.getId());
            return executeUpdate(sql, params.toArray());
        }
        return 0;
    }

    private String createPageCondition(User user, List<Object> params) {
        String sql = "";
        if(user != null){
            if(user.getId() != null){
                sql += " and id = ?";
                params.add(user.getId());
            }
            if(user.getRoleId() != null ){
                sql += " and roleId = ? ";
                params.add(user.getRoleId());
            }
            if(user.getLoginName() != null && !user.getLoginName().trim().isEmpty()){
                sql += " and loginName = ?";
                params.add(user.getLoginName());
            }
            if(user.getUserName() !=null && !user.getUserName().trim().isEmpty()){
                sql+=" and userName = ?";
                params.add(user.getUserName());
            }
            if(user.getPassword() !=null){
                sql+=" and password = ?";
                params.add(user.getPassword());
            }
            if(user.getSex()!=null){
                sql+=" and sex = ? ";
                params.add(user.getSex());
            }
            if(user.getIdentityCode()!=null && !user.getIdentityCode().trim().isEmpty()){
                sql+=" and identityCode = ?";
                params.add(user.getIdentityCode());
            }
            if(user.getEmail()!=null && !user.getEmail().trim().isEmpty()){
                sql+=" and email = ?";
                params.add(user.getEmail());
            }
            if(user.getMobile()!=null){
                sql+=" and mobile = ?";
                params.add(user.getMobile());
            }
            if(user.getEnabled()!=null){
                sql+=" and enabled = ?";
                params.add(user.getEnabled());
            }
            if(user.getCreateTime()!=null && !user.getCreateTime().trim().isEmpty()){
                sql+=" and createTime = ?";
                params.add(user.getCreateTime());
            }
            if(user.getUpdateTime()!=null && !user.getUpdateTime().trim().isEmpty()){
                sql+=" and updateTime = ?";
                params.add(user.getUpdateTime());
            }
            if(user.getAvatar()!=null && !user.getAvatar().trim().isEmpty()){
                sql+=" and avatar = ?";
                params.add(user.getAvatar());
            }
        }
        return sql;
    }
}
